package com.rpoonolly.simplegame.animation;

import java.io.IOException;
import java.util.Collections;

import javax.vecmath.Vector2d;

import com.rpoonolly.simplegame.Config;
import com.rpoonolly.simplegame.core.aabb.AABBEntity;
import com.rpoonolly.simplegame.core.aabb.EntityGameMap;
import com.rpoonolly.simplegame.textures.TextureId;

public class PlayerAnimator extends AnimationProcessor<AABBEntity, CharacterAnimation>  {
	
	public PlayerAnimator(EntityGameMap<AABBEntity> gameMap)
			throws IOException {
		super(gameMap, Config.PLAYER_TEXTURE_MAP, Collections.singletonList(Config.ENT_GRP_PLAYER));
	}

	@Override
	protected void loadAnimationCycles() {
		TextureId[] animationCycle;
		
		animationCycle = new TextureId[1];
		animationCycle[0] = new TextureId(animationMap, 0, 0);
		animations.put(CharacterAnimation.STAND_UP, animationCycle);
		
		animationCycle = new TextureId[1];
		animationCycle[0] = new TextureId(animationMap, 0, 1);
		animations.put(CharacterAnimation.STAND_LEFT, animationCycle);
		
		animationCycle = new TextureId[1];
		animationCycle[0] = new TextureId(animationMap, 0, 2);
		animations.put(CharacterAnimation.STAND_DOWN, animationCycle);
		
		animationCycle = new TextureId[1];
		animationCycle[0] = new TextureId(animationMap, 0, 3);
		animations.put(CharacterAnimation.STAND_RIGHT, animationCycle);
		
		animationCycle = new TextureId[animationMap.mapWidth-1];
		for(int i = 1; i < animationMap.mapWidth; i++)
				animationCycle[i-1] = new TextureId(animationMap, i, 0);
		animations.put(CharacterAnimation.WALK_UP, animationCycle);
		
		animationCycle = new TextureId[animationMap.mapWidth-1];
		for(int i = 1; i < animationMap.mapWidth; i++)
				animationCycle[i-1] = new TextureId(animationMap, i, 1);
		animations.put(CharacterAnimation.WALK_LEFT, animationCycle);
		
		animationCycle = new TextureId[animationMap.mapWidth-1];
		for(int i = 1; i < animationMap.mapWidth; i++)
				animationCycle[i-1] = new TextureId(animationMap, i, 2);
		animations.put(CharacterAnimation.WALK_DOWN, animationCycle);

		animationCycle = new TextureId[animationMap.mapWidth-1];
		for(int i = 1; i < animationMap.mapWidth; i++)
				animationCycle[i-1] = new TextureId(animationMap, i, 3);
		animations.put(CharacterAnimation.WALK_RIGHT, animationCycle);
		
		lastAnimation = CharacterAnimation.WALK_DOWN;
	}

	@Override
	protected void processAnimationFrame(AABBEntity entity) {
		TexturedDynamicEntity character = (TexturedDynamicEntity) entity;
		Vector2d v = character.getDirection();
		TextureId newAnimFrame;
		
		if (v.x == 0 && v.y == 0) {
			if (lastAnimation == CharacterAnimation.WALK_UP)
				newAnimFrame = getAnimationFrame(CharacterAnimation.STAND_UP);
			else if (lastAnimation == CharacterAnimation.WALK_LEFT)
				newAnimFrame = getAnimationFrame(CharacterAnimation.STAND_LEFT);
			else if (lastAnimation == CharacterAnimation.WALK_RIGHT)
				newAnimFrame = getAnimationFrame(CharacterAnimation.STAND_RIGHT);
			else if (lastAnimation == CharacterAnimation.WALK_DOWN)
				newAnimFrame = getAnimationFrame(CharacterAnimation.STAND_DOWN);
			else
				newAnimFrame = character.getCurrentTexture();
		} else if (Math.abs(v.x) > Math.abs(v.y)) {
			if (v.x > 0)
				newAnimFrame = getAnimationFrame(CharacterAnimation.WALK_RIGHT);
			else
				newAnimFrame = getAnimationFrame(CharacterAnimation.WALK_LEFT);
		} else {
			if (v.y > 0)
				newAnimFrame = getAnimationFrame(CharacterAnimation.WALK_UP);
			else
				newAnimFrame = getAnimationFrame(CharacterAnimation.WALK_DOWN);
		}
		
		character.setCurrentTexture(newAnimFrame);
	}
}
